package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.AccidentRecordEntity;
import city.spring.modules.oldman.service.AccidentRecordService;
import city.spring.modules.oldman.service.OldmanInfoService;
import city.spring.modules.oldman.vo.AccidentRecordVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Controller：意外记录
 *
 * @author 谢金婷
 * @date 2020-04-14 17:08:58
 */
@RestController
@RequestMapping("/accident_record")
public class AccidentRecordController extends ApiController {
    private final AccidentRecordService accidentRecordService;
    private final OldmanInfoService oldmanInfoService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<AccidentRecordEntity, ?>> entityOrderFields;

    public AccidentRecordController(AccidentRecordService accidentRecordService, OldmanInfoService oldmanInfoService) {
        this.accidentRecordService = accidentRecordService;
        this.oldmanInfoService = oldmanInfoService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(AccidentRecordEntity::getId);
        entityOrderFields.add(AccidentRecordEntity::getOldmanId);
        entityOrderFields.add(AccidentRecordEntity::getAccidentTime);
        entityOrderFields.add(AccidentRecordEntity::getWorkers);
        entityOrderFields.add(AccidentRecordEntity::getAccidentType);
        entityOrderFields.add(AccidentRecordEntity::getAccidentGrade);
        entityOrderFields.add(AccidentRecordEntity::getLiability);
        entityOrderFields.add(AccidentRecordEntity::getReason);
        entityOrderFields.add(AccidentRecordEntity::getDescription);
        entityOrderFields.add(AccidentRecordEntity::getPhoto);
    }

    /**
     * 获取全部的 <strong>意外记录</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, AccidentRecordVO entity) {
        LambdaQueryChainWrapper<AccidentRecordEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>意外记录</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, AccidentRecordVO entity, Date[] accidentTime) {
        Page<AccidentRecordEntity> page = MyBatisUtils.toPage(pageable);
        QueryWrapper<? extends AccidentRecordEntity> queryWrapper = buildQuery(entity);

        if (accidentTime != null && accidentTime.length == 2) {
            queryWrapper.ge("t1.accident_time", accidentTime[0]);
            queryWrapper.le("t1.accident_time", accidentTime[1]);
        }

        return success(accidentRecordService.listPage(page, queryWrapper));
    }

    /**
     * 获取一个 <strong>意外记录</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        Page<AccidentRecordEntity> page = new Page<>();
        QueryWrapper<AccidentRecordEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        IPage<AccidentRecordVO> iPage = accidentRecordService.listPage(page, wrapper);
        List<AccidentRecordVO> records = iPage.getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    /**
     * 添加一个 <strong>意外记录</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody AccidentRecordEntity entity) {
        accidentRecordService.saveAccidentRecord(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>意外记录</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody AccidentRecordEntity entity) {
        entity.setId(id);
        accidentRecordService.updateAccidentRecord(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>意外记录</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        accidentRecordService.deleteAccidentRecord(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>意外记录</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        accidentRecordService.deleteAccidentRecord(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<AccidentRecordEntity> buildLambdaQuery(AccidentRecordEntity entity) {
        LambdaQueryChainWrapper<AccidentRecordEntity> lambdaQuery = accidentRecordService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), AccidentRecordEntity::getId, entity.getId());
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getOldmanId()), AccidentRecordEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(entity.getAccidentTime() != null, AccidentRecordEntity::getAccidentTime, entity.getAccidentTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getWorkers()), AccidentRecordEntity::getWorkers, entity.getWorkers());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAccidentType()), AccidentRecordEntity::getAccidentType, entity
                .getAccidentType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAccidentGrade()), AccidentRecordEntity::getAccidentGrade, entity
                .getAccidentGrade());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getLiability()), AccidentRecordEntity::getLiability, entity.getLiability());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReason()), AccidentRecordEntity::getReason, entity.getReason());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDescription()), AccidentRecordEntity::getDescription, entity.getDescription());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPhoto()), AccidentRecordEntity::getPhoto, entity.getPhoto());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query wrapper
     */
    private QueryWrapper<? extends AccidentRecordEntity> buildQuery(AccidentRecordVO entity) {
        QueryWrapper<? extends AccidentRecordEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.eq(StringUtils.isNotBlank(entity.getOldmanId()), "t1.oldman_id", entity.getOldmanId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanName()), "t2.oldman_name", entity.getOldmanName());
        wrapper.eq(entity.getAccidentTime() != null, "t1.accident_time", entity.getAccidentTime());
        wrapper.like(StringUtils.isNotBlank(entity.getWorkers()), "t1.workers", entity.getWorkers());
        wrapper.like(StringUtils.isNotBlank(entity.getAccidentType()), "t1.accident_type", entity.getAccidentType());
        wrapper.like(StringUtils.isNotBlank(entity.getAccidentGrade()), "t1.accident_grade", entity.getAccidentGrade());
        wrapper.like(StringUtils.isNotBlank(entity.getLiability()), "t1.liability", entity.getLiability());
        wrapper.like(StringUtils.isNotBlank(entity.getReason()), "t1.reason", entity.getReason());
        wrapper.like(StringUtils.isNotBlank(entity.getDescription()), "t1.description", entity.getDescription());
        wrapper.like(StringUtils.isNotBlank(entity.getPhoto()), "t1.photo", entity.getPhoto());
        return wrapper;
    }
}
